package mannA1.view;
import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.JTextComponent;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.Canvas;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import mannA1.controller.DrawEdgeController;
import mannA1.controller.InsertChoicesController;
import mannA1.model.Model;
import javax.swing.JButton;

public class A1GUI extends JFrame {

	private JPanel finalDecision;
	public JTextField[] choices;

	
	/** keep track of model */
	Model model;
	private LinesCanvas canvas;

	/**
	 * Create the frame.
	 */
	public A1GUI(Model m) 
{
		this.model = m;
		canvas = new LinesCanvas(m);
		canvas.addMouseListener(new DrawEdgeController(this, m, canvas));

		setTitle("Decision Lines");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 533, 450);
		finalDecision = new JPanel();
		finalDecision.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(finalDecision);
		
		JPanel panel_1 = new JPanel();
		
		
		/** Create an array of JTextFields, for the user input choices.*/
		JPanel ChoicesPanel = new JPanel();
		ChoicesPanel.setLayout(null);
		choices = new JTextField[m.numLines];
		
		
		/** Chooses a location for the JTextFields.*/
		  for (int i = 0; i < m.numLines; i++)
		  {
			  int s = 300/m.numLines;
			  
              choices[i] = new JTextField ();
              choices[i].setBounds(s-15 + i*s, 10, 40, 20);
              ChoicesPanel.add(choices[i]);
              
              
              final int position = i;
              choices[i].addActionListener(new ActionListener()
    		  {
    				@Override
    				public void actionPerformed(ActionEvent ae)
    				{
    					new InsertChoicesController(A1GUI.this, model, position).process();
    				}
    		});
		  }
		
		/** This is the button for when to begin inserting Edges, and to stop users from changing choices. */  
		final int position2 = 0;
		JButton btnNewButton = new JButton("Begin Rounds");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				new InsertChoicesController(A1GUI.this, model, position2).noMoreChoices();
			}
		});

				
		
		GroupLayout gl_finalDecision = new GroupLayout(finalDecision);
		gl_finalDecision.setHorizontalGroup(
			gl_finalDecision.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_finalDecision.createSequentialGroup()
					.addGap(54)
					.addGroup(gl_finalDecision.createParallelGroup(Alignment.TRAILING, false)
						.addComponent(ChoicesPanel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(panel_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE))
					.addPreferredGap(ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
					.addComponent(btnNewButton)
					.addContainerGap())
		);
		gl_finalDecision.setVerticalGroup(
			gl_finalDecision.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_finalDecision.createSequentialGroup()
					.addGroup(gl_finalDecision.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_finalDecision.createSequentialGroup()
							.addComponent(ChoicesPanel, GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 341, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_finalDecision.createSequentialGroup()
							.addGap(67)
							.addComponent(btnNewButton)))
					.addContainerGap())
		);
		panel_1.add(canvas);
		finalDecision.setLayout(gl_finalDecision);
		  
	}
	
	
	/** Get method for the JTextFields. */
	public JTextField getChoice(int i) 
	  {
			return choices[i];
	  }
}
